package com.waymap.controller;

import com.waymap.common.Constant;
import com.waymap.domain.Menu;
import com.waymap.domain.User;
import com.waymap.service.MenuService;
import com.waymap.service.UserService;
import com.waymap.utils.AuthonTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by tim on 2018/7/18.
 */
@CrossOrigin
@RestController
@RequestMapping("/menu")
@Api(value = "WMS系统", tags = {"WMS-菜单"})
public class MenuController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @SuppressWarnings({"unchecked", "rawtypes"})
    @ApiOperation(value = "获取客户菜单")
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity getMenu(
            @ApiParam(value = "token") @RequestParam(required = true) String token,
            @ApiParam(value = "userId") @RequestParam(required = true) String userId) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        }
        User currentUser = userService.getUserInfoByToken(token);
        List<Menu> menus = menuService.getMenuByAccountId(currentUser.getAccountId());
        return ResponseEntity.ok(menus);
    }


}